<?php
/**
 * 后台游戏表操作模型
 * @author heyi
 */
class MGame extends MY_Model {

	const STATE_ENABLE = 1;
	const STATE_DISABLE = 0;
	protected $table = 'game';
	private $filter = array();

    function __construct() {
        parent::__construct();
    }
    
    function getState($state=NULL){
    	$define = array(self::STATE_DISABLE=>'禁用',self::STATE_ENABLE=>'启用');
    	if($state === NULL){
    		return $define;
    	}else{
    		return key_exists($state, $define)?$define[$state]:'';
    	}
    }
    
    public function getGameAndTags($where = null){
    	$this->db->select($this->table.'.*,game_tags.tag_id as tag_id');
		$this->db->from($this->table);
		$this->db->join('game_tags', $this->table.'.id=game_tags.game_id','left');
		if($where != null){
			$this->db->where($where);
		}
		return $this->db->get()->row();
    }
    
	public function getList($limit, $sort=false){
		$this->db->select( $this->table.'.*');
		$this->db->distinct();
		$this->db->from($this->table);
		$this->db->join('game_tags', $this->table.'.id=game_tags.game_id','LEFT');
		$this->db->limit($limit['limit'],$limit['offset']);
		$this->formfilter->doFilter();
		if($sort){
			$this->db->order_by('sort', 'asc');
		}else{
			$this->db->order_by('id', 'desc');
		}
		$query = $this->db->get();
		return $query->result();
	}
	public function getCount(){
		$this->db->select('count(distinct g.id) as num');
		$this->db->from($this->table . ' as g');
		$this->db->join('game_tags as t', 'g.id=t.game_id','LEFT');
		$this->formfilter->doFilter();
		$tmp = $this->db->get()->result();
		return (int)$tmp[0]->num;
	}
	
	/**
     * 
     * 根据游戏ID查询游戏信息
     * @param $id 游戏ID
     */
	public function getById($id){
		$query = $this->db->where('id',$id)->get($this->table);
		return $query->row();
	}
	
	/**
	 * 添加游戏时事务处理
	 * Enter description here ...
	 * @param unknown_type $newObj
	 * @param unknown_type $post_tag_id_arr
	 */
	public function handleAddTrans($newObj,$post_tag_id_arr){
		$this->db->trans_start();
		if(!$this->MGame->add($newObj)){
			$this->db->_trans_status = FALSE;
		}
		$game_id = $newObj->id;
		if(count($post_tag_id_arr)>0){
			foreach ($post_tag_id_arr as $tag_id){
				$game_tag_obj = $this->MGameTags->createVo();
				$game_tag_obj->game_id = $game_id;
				$game_tag_obj->tag_id = $tag_id;
				if(!$this->MGameTags->add($game_tag_obj)){
					$this->db->_trans_status = FALSE;
				}
			}
		}
		$this->db->trans_complete();
		if($this->db->trans_status() !== FALSE){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	/**
	 * 编辑游戏时事务处理
	 * Enter description here ...
	 * @param $id
	 * @param $newObj
	 * @param $post_tag_id_arr
	 */
	public function handleEditTrans($id,$newObj,$post_tag_id_arr){
		$this->db->trans_start();
		//删除掉该游戏原来所有tag
		if(!$this->MGameTags->deleteTagsOfGameId($id)){
			$this->db->_trans_status = FALSE;
		}
		if(!$this->MGame->update($newObj)){
			$this->db->_trans_status = FALSE;
		}
		if(count($post_tag_id_arr)>0){
			foreach ($post_tag_id_arr as $tag_id){
				$game_tag_obj = $this->MGameTags->createVo();
				$game_tag_obj->game_id = $id;
				$game_tag_obj->tag_id = $tag_id;
				if(!$this->MGameTags->add($game_tag_obj)){
					$this->db->_trans_status = FALSE;
				}
			}
		}
		$this->db->trans_complete();
		if($this->db->trans_status() !== FALSE){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	/**
	 * 删除游戏时事务处理
	 * Enter description here ...
	 * @param unknown_type $id
	 * @param unknown_type $update_time
	 */
	public function handleDeleteTrans($id,$update_time){
		$this->db->trans_start();
		if(!$this->MGame->delete($id, $update_time)){
			$this->db->_trans_status = FALSE;
		}
		if(!$this->MGameTags->deleteGameId($id)){
			$this->db->_trans_status = FALSE;
		}
		$this->db->trans_complete();
		if($this->db->trans_status() !== FALSE){
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	
}
